package com.kongwg.java3;

import com.kongwg.java2.Employee;
import com.kongwg.java2.EmployeeData;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author kongwg
 * @create 2020-12-25 20:10
 */
public class StreamAPITest {

    @Test
    public void test1(){

        List<Employee> employees = EmployeeData.getEmployees();
        Stream<Employee> stream = employees.stream();

        //切片
        //过滤
        stream.filter(e -> e.getSalary() > 7000).forEach(System.out::println);
        System.out.println();
        //只保留前三个
        employees.stream().limit(3).forEach(System.out::println);
        System.out.println();
        //跳过
        employees.stream().skip(3).forEach(System.out::println);
        System.out.println();

        //去重
        employees.add(new Employee(1001,"刘强东",40,8888));
        employees.add(new Employee(1001,"刘强东",40,8888));
        employees.add(new Employee(1001,"刘强东",40,8888));
        employees.add(new Employee(1001,"刘强东",40,8888));
        employees.add(new Employee(1001,"刘强东",40,8888));

        employees.stream().distinct().forEach(System.out::println);
    }

    //映射
    //map(Function f)——接收一个函数作为参数，将元素转换成其他形式或提取信息，该函数会被应用到每个元素上，并将其映射成一个新的元素。
    @Test
    public void test2(){
        List<String> list = Arrays.asList("aa", "bb", "cc", "dd");
        Stream<String> stream = list.stream();
        stream.map(str -> str.toUpperCase()).forEach(System.out::println);


        System.out.println();
//        练习1：获取员工姓名长度大于3的员工的姓名。
        List<Employee> employees = EmployeeData.getEmployees();
        Stream<Employee> stream1 = employees.stream();
        Stream<String> nameStream = stream1.map(Employee::getName);
        nameStream.filter(name -> name.length() > 3).forEach(System.out::println);

        Stream<Character> characterStream = list.stream().flatMap(StreamAPITest::formStringToStream);
        characterStream.forEach(System.out::println);

    }
    //将字符串中的多个字符构成的集合转换为对应的Stream的实例
    public static Stream<Character> formStringToStream(String str){

        ArrayList<Character> list = new ArrayList<>();

        for (Character c : str.toCharArray()){
            list.add(c);
        }
        return list.stream();

    }

    /**
     * 排序
     */
    @Test
    public void test3(){
        List<Integer> list = Arrays.asList(-29, 283, 83, 82, 299);

        list.stream().sorted().forEach(System.out::println);

        List<Employee> employees = EmployeeData.getEmployees();
        employees.stream().sorted((e1,e2) ->{
            int aget = Integer.compare(e1.getAge(), e2.getAge());
            if (aget != 0){
                return aget;
            }else {
                return Double.compare(e1.getSalary(), e2.getSalary());
            }

        }).forEach(System.out::println);
    }

    @Test
    public void test5(){

        Stream.iterate(0,x -> x + 1).limit(10).forEach(System.out::println);


        //java9中新增的重载的方法
        Stream.iterate(0,x -> x < 100,x -> x + 1).forEach(System.out::println);
    }
}
